package com.example.demo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;



public class RabbitMqConfig {
		
		@Autowired
		private QueueConfig queueConfig;
		@Autowired
	    private ExchangeConfig exchangeConfig;
		
		 /*
	    	将登录消息队列和交换机进行绑定
		  */
	   
		public Binding loginbindingExchangeMessage(){
			return BindingBuilder.bind(queueConfig.loginQueue()).to(exchangeConfig.topicExchange()).with("topic.login");
		}
		
		/*
		将注册消息队列和交换机进行绑定
	    */
		public Binding registerbindingExchangeMessage(){
			return BindingBuilder.bind(queueConfig.registerQueue()).to(exchangeConfig.topicExchange()).with("topic.register");
		}
		
		/*
		将忘记密码消息队列和交换机进行绑定
	    */
		public Binding forgetbindingExchangeMessage(){
			return BindingBuilder.bind(queueConfig.forgetQueue()).to(exchangeConfig.topicExchange()).with("topic.forget");
		}
		/*
		 * 传递验证码
		 * */
		public Binding forgetbindingMessage(){
			return BindingBuilder.bind(queueConfig.forgetQueue()).to(exchangeConfig.topicExchange()).with("topic.random");
		}
		@Bean
		public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
		        RabbitTemplate template = new RabbitTemplate(connectionFactory);
		        template.setMessageConverter(new Jackson2JsonMessageConverter());
		        return template;
		    }
		 @Bean
		    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
		        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
		        factory.setConnectionFactory(connectionFactory);
		        factory.setMessageConverter(new Jackson2JsonMessageConverter());
		        return factory;
		    }
}
